package cn.kgc.controller;

import cn.kgc.common.CrowdFundingConstant;
import cn.kgc.common.ResultEntity;
import cn.kgc.pojo.User;
import cn.kgc.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-03-08 14:50
 */
@Controller
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("/user/query/for/search")
    public String queryForSearch(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            Model model) {
        PageInfo<User> pageInfo = userService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "body/user/user-member-list";
    }

    //详细信息
    @RequestMapping("/select/By/id")
    public String selectid(@RequestParam("id") Integer id, Model model) {
        User xiangqing = userService.selectByIdUser(id);

        model.addAttribute("xiang", xiangqing);
        return "body/user/user-xiang";
    }


    //删除回显 批量
    //根据id查询
    @ResponseBody
    @RequestMapping("/user/get/list/by/id/list")
    public ResultEntity<List<User>> getRoleListByIdList(@RequestBody List<Integer> userIdList) {
        List<User> userListByIdList = userService.getUserListByIdList(userIdList);
        return ResultEntity.successWithoutData(userListByIdList);
    }

    //根据id删除  批量
    @ResponseBody
    @RequestMapping("/user/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> userIdList) {
        userService.batchRemove(userIdList);
        return ResultEntity.successWithoutData();
    }

    //单条删除
    @RequestMapping("delete/user")
    public String deleteblog(@RequestParam("id") Integer id, Integer pageNum) {
        userService.remove(id);
        return "redirect:/user/query/for/search";
    }


//    //添加
//    @RequestMapping("/user/batch/add")
//    public String userbatchadd(User user) {
//        userService.addUser(user);
//        return "redirect:user/query/for/search";
//    }

    //增加
    @RequestMapping("/user/save")
    public String saveAdmin(User user, Integer pageNum) {
        userService.saveUser(user);
        //删除成功跳转到查询页面  分页查询
        return "redirect:/user/query/for/search";
    }

    //详细信息
    @RequestMapping("/select/id")
    public String selectids(@RequestParam("id") Integer id, Model model) {
        User xiangqing = userService.selectByIdUser(id);

        model.addAttribute("xiang", xiangqing);
        return "body/user/user-update";
    }


    //修改
    @RequestMapping("/user/update")
    public String updateAdmin(User user, Integer pageNum) {
        userService.updateUser(user);
        //当前页 存储过去
        return "redirect:/user/query/for/search";
    }


    //个人信息
    @RequestMapping("/ge/ren/user")
    public String  gerenUser(Integer id,Model model){
        User user = userService.selectById(id);
        model.addAttribute("user",user);
        return "body/geren/personage";
    }

    //完善个人信息 修改
    @RequestMapping("/update/user/id")
    public String updateuserid(User user){
        userService.update(user);
        return "/body/geren/personage";
    }


    //修改密码
    @RequestMapping("/ge/ren/user/update/password")
    public String  gerenUserupdatepassword(Integer id,Model model){
        User user = userService.selectById(id);
        model.addAttribute("user",user);
        return "body/geren/update-Password";
    }

    //完善个人信息 修改
    @RequestMapping("/update/password")
    public String updatepassword(User user){
        userService.update(user);
        return "index";
    }

    //账号注销
    @RequestMapping("/delete/geren")
    public String  deletegeren(Integer id,Model model){
        User user = userService.selectById(id);
        model.addAttribute("user",user);
        return "body/geren/delete-write";
    }

    //账号注销
    @RequestMapping("/delete/user/geren")
    public String deleteusergeren(@RequestParam("id") Integer id, Integer pageNum, HttpSession session) {
        userService.remove(id);
        session.invalidate();
        return "index";
    }
}
